package com.jlf.core.engine.persistence.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jlf.core.engine.dao.SchemaFieldDao;
import com.jlf.core.engine.domain.SchemaFieldDO;
import com.jlf.core.engine.persistence.SchemaFieldPersistence;
import com.jlf.core.mp.persistence.impl._SimplePersistenceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
import java.util.List;

/**
 * 领域字段定义的持久化服务
 *
 * @author wujr
 * 2023/7/5
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/5 1.0 新增]
 */
@Slf4j
@RequiredArgsConstructor
public class SchemaFieldPersistenceImpl extends _SimplePersistenceImpl<SchemaFieldDao, SchemaFieldDO> implements SchemaFieldPersistence {
    @Override
    public List<SchemaFieldDO> loadByDomain(String domainCode, Collection<String> classFieldCodes) {
        QueryWrapper<SchemaFieldDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SchemaFieldDO::getDomainCode, domainCode)
                .in(!CollectionUtils.isEmpty(classFieldCodes), SchemaFieldDO::getClassFieldCode, classFieldCodes)
                .orderByAsc(SchemaFieldDO::getPriority);
        return super.list(wrapper);
    }

    @Override
    public List<SchemaFieldDO> loadByDomains(Collection<String> domainCodes, Integer status) {
        QueryWrapper<SchemaFieldDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(SchemaFieldDO::getDomainCode, domainCodes)
                .eq(status != null, SchemaFieldDO::getStatus, status)
                .orderByAsc(SchemaFieldDO::getPriority);
        return super.list(wrapper);
    }

    @Override
    public void removeByDomain(String domainCode) {
        QueryWrapper<SchemaFieldDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SchemaFieldDO::getDomainCode, domainCode);
        super.remove(wrapper);
    }
}
